const app = getApp()
const ajax = require('../../utils/ajax')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tablist: [],
    bechoice: 0,
    goodsList: [],
    firstRequest: true,
    searchInput:"",
  },

  searchInput(e){
    this.setData({
      searchInput: e.detail.value
    })
  },

  search(){
    if(this.data.searchInput == ""){
      wx.showToast({
        title: '请输入搜索内容',
        icon:"none",
        duration:1200
      })
      return false
    }
    wx.navigateTo({
      url: '/pages/searchResult/searchResult?searchInpu='+this.data.searchInput,
    })
  },

  choiceType(e){
    let that = this
    if (that.data.bechoice == e.currentTarget.dataset.id){
      return false;
    }
    that.setData({
      bechoice: e.currentTarget.dataset.id,
      goodsList: []
    })
    this.getGoodsList(this.data.bechoice)
  },

  godetail(e){
    let {goods_id} = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/details/details?goods_id=${goods_id}`,
    })
  },
  /**
   * @description: 获取商品列表
   * @param {type}  type=商品类型
   * @return:
   */
  getGoodsList(type) {
    let that = this
    ajax.wxRequest({
      url: '/index.php/Api/SApi/index/m/getGoodsList',
      method:"GET",
      data: {
        type_id: type,
        is_select: 1,
        btype: 1,
        is_old: 1
      }
    }).then(res => {
      res.datas && that.setData({
        tablist: res.selected_type,
        goodsList: that.data.goodsList.concat(res.datas)
      })
      if (that.data.firstRequest) {
        that.setData({
          bechoice: res.selected_type[0]['id'],
          firstRequest: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const _this = this
    this.getGoodsList(this.data.bechoice)
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {


  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: "PAPARELA  我的御用化妆师",
    }
  }
})
